package com.lcxw.hxzbapi.dao.mapper;

import com.lcxw.hxzbapi.entity.pojo.Attention;
import com.lcxw.hxzbapi.entity.pojo.YwpdMember;
import java.util.List;

import com.lcxw.hxzbapi.entity.response.MemberResp;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Component
@Mapper
@Service
public interface AttentionMapper {

    /**
     * 通过userId来查询该用户关注了哪些用户
     */
    @Select("SELECT attuid FROM ywpd_attention WHERE userid = #{userId} AND attype = 'member' and status=0")
    List<Long> selectAttuidByUserId(long userId);

    @Select("SELECT * FROM ywpd_member WHERE userId = ANY(SELECT attuId FROM ywpd_attention WHERE userId = #{userId} AND attype='member' and status=0) ")
    List<YwpdMember> getAtts(long userId);

    @Select("SELECT attuid FROM ywpd_attention WHERE userid = #{userId} AND attype = 'room'")
    List<Long> selectAttroomidByUserId(long userId);

    /**
     * 通过attuid来查询该用户被多少人关注
     */
    @Select("SELECT COUNT(attuid) FROM ywpd_attention WHERE attuid = #{attuid} and status=0")
    int selectCountOfAttuid(long attuid) throws Exception;

    /**
     * 添加一个关注的人
     */
    @Insert("INSERT INTO ywpd_attention (userid,attuid,attype,addtime) VALUES (#{userId},#{attuId},#{attype},#{addTime})")
    int addAttentionUser(@Param("userId") long userId, @Param("attuId") long attuId,
        @Param("attype") String attype, @Param("addTime") String addTime) throws Exception;

    @Delete("DELETE FROM ywpd_attention where userid=#{userId} and attuid=#{attuId} and attype=#{attype} ")
    int deleteAttentionUser(@Param("userId") long userId, @Param("attuId") long attuId,
        @Param("attype") String attype) throws Exception;

    @Select("SELECT * FROM ywpd_attention WHERE userid=#{userId} and attuid=#{attuId} and attype=#{attype} and status=0")
    Attention getAttentionByUserIdAndAttuId(@Param("userId") long userId,
        @Param("attuId") long attuId, @Param("attype") String attype);

    //根据userId和attuId查询我是否拉黑这个人
    @Select("SELECT * FROM ywpd_attention WHERE userid=#{userId} and attuid=#{attuId}  and status=#{status}")
    Attention getAttentionByUserIdStatus(@Param("userId") long userId,
                                            @Param("attuId") long attuId, @Param("status") int status);


    @Select("SELECT userId FROM ywpd_attention WHERE  attuid=#{attuId} and attype=#{attype} and status=0")
    List<Long> getFans(@Param("attuId") long attuId, @Param("attype") String attype);

    @Select("SELECT  * FROM ywpd_member WHERE userId = ANY( SELECT userId FROM ywpd_attention WHERE attuId = #{userId} and attype='member' and status=0)")
    List<YwpdMember> getFans2(@Param("userId") long userId);

    /**
     * 查询好友
     */
    @Select("SELECT * FROM ywpd_member WHERE userid = ANY(SELECT attuid FROM ywpd_attention WHERE attuid = ANY(SELECT userid FROM `ywpd_attention` WHERE attuid =  #{userId} and attype='member') AND userId = #{userId} and status=0)")
    List<YwpdMember> getFriends(long userId);

    /**
     * 查询粉丝数量
     */
    @Select("SELECT COUNT(*) FROM ywpd_attention WHERE attuid=#{attuId} AND attype = 'member' and status=0")
    int getFansNum(long attuId);

    /**
     * 查询我关注的人的数量
     */
    @Select("SELECT COUNT(*) FROM ywpd_attention WHERE userId=#{userId} AND attype = 'member' and status=0")
    int getAttNum(long userId);

    /**
     * 查询好友的数量
     */
    @Select("SELECT COUNT(attuId) FROM ywpd_attention WHERE attuId = ANY(SELECT userId FROM `ywpd_attention` WHERE attuId =  #{userId} and status=0) AND userId = #{userId} and status=0")
    int getFriendNum(long userId);


    //根据userId和attuId将用户拉入黑名单
    @Update("UPDATE  ywpd_attention set status=1,statusTime=#{statusTime} WHERE userid=#{userId} and attuid=#{attuId}  AND attype = 'member'")
    int addOne(@Param("userId") long userId, @Param("attuId") long attuId, @Param("statusTime") String statusTime);

    //根据用户查询黑名单列表
    @Select("SELECT a.statusTime,b.nickName,b.imgUrl FROM (SELECT attuid,statusTime FROM ywpd_attention WHERE attuid = ANY(SELECT userid FROM `ywpd_attention` WHERE attuid =  #{userId} and attype='member') AND userId =#{userId}  and status=1) a LEFT JOIN ywpd_member b on a.attuid=b.userId")
    List<MemberResp> lists(@Param("userId") long userId);

    //根据userId和attuId将用户移出黑名单
    @Update("UPDATE  ywpd_attention set status=0 WHERE userid=#{userId} and attuid=#{attuId}  AND attype = 'member'")
    int descOne(@Param("userId") long userId, @Param("attuId") long attuId);


}
